//
//  SHStatusPictureView.swift
//  SHWeibo01
//
//  Created by 卫平玉 on 16/7/25.
//  Copyright © 2016年 卫平玉. All rights reserved.
//

import UIKit

let SHStatusPictureViewCellId = "SHStatusPictureViewCell"

let cellMargin: CGFloat = 5

let cellWH: CGFloat = 80

class SHStatusPictureView: UICollectionView {

    var pictureInfo: [SHPictureInfo]?{
        
        didSet{
            
            countLabel.text = "\(pictureInfo?.count ?? 0)"
            
            let size = getPictureViewSize(pictureInfo?.count ?? 0)
            
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
            
            reloadData()
        }
        
    }
    
  private func getPictureViewSize(count: Int) ->CGSize{
        
        let col = count == 4 ? 2 : count >= 3 ? 3 : count
        
        let row = count == 4 ? 2 : (count - 1)/3 + 1
    
    let w = CGFloat(col) * cellWH + CGFloat(col - 1) * cellMargin
    
    let h = CGFloat(row) * cellWH + CGFloat(row - 1) * cellMargin
    
    return CGSize(width: w, height: h)
    }
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    private func setupUI(){
        
        backgroundColor = UIColor.clearColor()
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        
        layout.minimumInteritemSpacing = cellMargin
        
        layout.minimumLineSpacing = cellMargin
        
        registerClass(SHStatusPictureViewCell.self, forCellWithReuseIdentifier: SHStatusPictureViewCellId)
        
        dataSource = self
        
        //添加控件
        addSubview(countLabel)
        
        //添加约束
        countLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
    }
    
    //懒加载控件
    private lazy var countLabel: UILabel = UILabel(textColor: UIColor.redColor(), textFont: 35)
}

extension SHStatusPictureView: UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureInfo?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(SHStatusPictureViewCellId, forIndexPath: indexPath) as! SHStatusPictureViewCell
        cell.pic_url = pictureInfo![indexPath.item]
        
        return cell
    }
    
}

class SHStatusPictureViewCell: UICollectionViewCell {
    
    var pic_url: SHPictureInfo?{
        
        didSet{
            
            bigImageView.sh_setImage(pic_url?.thumbnail_pic, placeholderImgName:"weibo_placeholder")
            
            if let thPic = pic_url?.thumbnail_pic where thPic.hasSuffix(".gif"){
                
                gifImageView.hidden = false
            }else{
                
                gifImageView.hidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        
        backgroundColor = RandomColor()
        
        contentView.addSubview(bigImageView)
        
        contentView.addSubview(gifImageView)
        
        
        bigImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
    }
    
    
    //背景图片
    private lazy var bigImageView: UIImageView = {
        
        let img = UIImageView(imgName: "weibo_placeholder")
        
        img.contentMode = .ScaleAspectFill
        
        img.layer.masksToBounds = true
        
        return img
    }()
    
    //动态图片logo
    private lazy var gifImageView: UIImageView = UIImageView(imgName: "timeline_image_gif")
    
}
